package com.itqf.service.impl;

import com.itqf.dao.CartDao;
import com.itqf.dao.OrderDao;
import com.itqf.dao.UserDao;
import com.itqf.dao.impl.CartDaoImpl;
import com.itqf.dao.impl.OrderDaoImpl;
import com.itqf.dao.impl.UserDaoImpl;
import com.itqf.entity.Cart;
import com.itqf.entity.Item;
import com.itqf.entity.Orders;
import com.itqf.entity.User;
import com.itqf.service.OrderService;
import com.itqf.utils.RandomUtils;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class OrderServiceImpl implements OrderService {

    private static OrderDao orderDao = new OrderDaoImpl();
    private static CartDao cartDao = new CartDaoImpl();
    private static UserDao userDao = new UserDaoImpl();

    @Override
    public void createOrder(String aid, String uid, String sum) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.创建一个订单对象进行保存
        Orders orders = new Orders();

        BigDecimal bsum = new BigDecimal(sum);
        String orderId = RandomUtils.createOrderId();//随机创建订单ID
        orders.setOid(orderId);
        orders.setAid(Integer.parseInt(aid));
        orders.setUid(Integer.parseInt(uid));
        orders.setOtime(new Date());
        orders.setOcount(bsum);
        orders.setOstate(1);//未支付状态
        //2.保存订单
        orderDao.insertOrders(orders);

        //3.将购物车转成订单项

        List<Cart> carts = cartDao.selectCartsByUid(Integer.parseInt(uid));

        List<Item> items = new ArrayList<>();
        for (Cart cart : carts) {
            Item item = new Item();
            item.setOid(orderId);
            item.setPid(cart.getPid());
            item.setInum(cart.getCnum());
            item.setIcount(cart.getCcount());
            items.add(item);
        }

        //4.保存订单对应的订单项
        orderDao.insertItems(items);

        //5.清空购物车
        cartDao.deletCartByUid(uid);
    }

    @Override
    public List<Orders> findOrdersByUid(int uid) throws IllegalAccessException, SQLException, InvocationTargetException {

        List<Orders> ordersList = orderDao.selectOrderByUid(uid);
        return ordersList;
    }

    @Override
    public Orders findOrdersByOid(String oid) throws IllegalAccessException, SQLException, InvocationTargetException {
        //1.oid查询订单和订单地址信息
        Orders orders = orderDao.selectOrderByOid(oid);
        //2.oid对应的对应订单项和商品信息
        List<Item> items = orderDao.selectItemsByOid(oid);
        //3.订单项集合设置给订单对象
        orders.setItems(items);
        return orders;
    }

    @Override
    public void updateStateByOid(String oid) throws SQLException {

        orderDao.updateStateByOid(oid);
    }

    @Override
    public List<Orders> findAllOrders() throws IllegalAccessException, SQLException, InvocationTargetException {

        List<Orders> ordersList = orderDao.findAllOrders();
        return ordersList;
    }

    @Override
    public void changeStateByOid(String oid) throws SQLException {
        orderDao.changeStateByOid(oid);
    }

    @Override
    public List<Orders> selectOrderByUsernameAndOstate(String username, String ostate) throws SQLException, InvocationTargetException, IllegalAccessException {

        //1.先根据username查询用户数据，获取uid(与orders关联的主键)
        User user = userDao.selectUserByUname(username);
        int uid = user.getUid();

        //2.根据uid和ostate来查询订单数据
        List<Orders> ordersList = orderDao.selectOrderByOidAndOstate(uid, ostate);
        System.out.println("业务层查询到的数据ordersList"+ordersList);
        return ordersList ;
    }
}
